<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水日期">
        <el-date-picker
          v-model="daterangeFlowDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="门店类型" prop="storeId">
        <el-select v-model="queryParams.storeId"  placeholder="请选择门店类型">
            <el-option
              v-for="dict in storeListOptions"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId"  placeholder="请输入项目名称">
            <el-option
              v-for="dict in bankProjectListOptions"
              :key="dict.id"
              :label="dict.projectName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="银行" prop="bankId">
        <el-select v-model="queryParams.bankId"  placeholder="请选择银行">
            <el-option
              v-for="dict in bankListOptions"
              :key="dict.id"
              :label="dict.bankName"
              :value="parseInt(dict.id)"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出账类别" prop="outcomeId">
        <el-select v-model="queryParams.outcomeId"  placeholder="请输入出账类别">
            <el-option
              v-for="dict in bankOutcomeListOptions"
              :key="dict.id"
              :label="dict.outcomeType"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="出账名目" prop="outcomeNameId">
        <el-select v-model="queryParams.outcomeNameId"  placeholder="请输入出账名目">
            <el-option
              v-for="dict in bankOutcomeNameListOptions"
              :key="dict.id"
              :label="dict.outcomeName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="支出对象" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入支出对象"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplier:reportOutFlow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplier:reportOutFlow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplier:reportOutFlow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['supplier:reportOutFlow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportOutFlowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="55" />
      <el-table-column label="流水日期" align="center" prop="flowDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flowDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店类型" align="center" prop="storeName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="银行" align="center" prop="bankName" />
      <el-table-column label="出账类别" align="center" prop="outcomeType" />
      <el-table-column label="出账名目" align="center" prop="outcomeName" />
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="支出对象" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplier:reportOutFlow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplier:reportOutFlow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改每日营业进账流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流水日期" prop="flowDate">
          <el-date-picker clearable size="small"
            v-model="form.flowDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择流水日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="门店类型" prop="storeId">
          <el-select v-model="form.storeId"  placeholder="请选择门店类型">
            <el-option
              v-for="dict in storeListOptions"
              :key="dict.id"
              :label="dict.name"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectId">
          <el-select v-model="form.projectId"  placeholder="请输入项目名称">
            <el-option
              v-for="dict in bankProjectListOptions"
              :key="dict.id"
              :label="dict.projectName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行" prop="bankId">
          <el-select v-model="form.bankId"  placeholder="请选择银行">
            <el-option
              v-for="dict in bankListOptions"
              :key="dict.id"
              :label="dict.bankName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出账类别" prop="outcomeId">
          <el-select v-model="form.outcomeId"  placeholder="请输入出账类别">
            <el-option
              v-for="dict in bankOutcomeListOptions"
              :key="dict.id"
              :label="dict.outcomeType"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出账名目" prop="outcomeNameId">
          <el-select v-model="form.outcomeNameId"  placeholder="请输入出账名目">
            <el-option
              v-for="dict in bankOutcomeNameListOptions"
              :key="dict.id"
              :label="dict.outcomeName"
              :value="parseInt(dict.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="price">
           <el-input-number controls-position="right" v-model="form.price" placeholder="请输入金额" :min="0" :max="10000000" :step="1" :precision="2"/>
        </el-form-item>
        <el-form-item label="支出对象" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入支出对象" maxlength="30"  show-word-limit />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReportOutFlow, getReportOutFlow, delReportOutFlow, addReportOutFlow, updateReportOutFlow, exportReportOutFlow } from "@/api/supplier/reportOutFlow";
import { listStore } from "@/api/supplier/store";
import { listBank} from "@/api/supplier/bank";
import { listBankOutcome } from "@/api/supplier/bankOutcome";
import { listBankOutcomeName } from "@/api/supplier/bankOutcomeName";
import { listBankProject } from "@/api/supplier/bankProject";
export default {
  name: "ReportOutFlow",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每日营业出账流水表格数据
      reportOutFlowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 流水日期时间范围
      daterangeFlowDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowDate: null,
        storeId: null,
        projectId: null,
        bankId: null,
        outcomeId: null,
        outcomeNameId: null,
        price: null,
        userName: null,
      },
      //门店类型
      storeListOptions:[],
      //银行
      bankListOptions:[],
      //出账类别
      bankOutcomeListOptions:[],
      //出账名目
      bankOutcomeNameListOptions:[],
      //项目
      bankProjectListOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeId: [
          { required: true, message: "门店类型不能为空" }
        ],
        flowDate: [
          { required: true, message: "流水日期不能为空" }
        ],
        projectId: [
          { required: true, message: "项目名称不能为空" }
        ],
        outcomeId: [
          { required: true, message: "出账类别不能为空" }
        ],
        outcomeNameId: [
          { required: true, message: "出账名目不能为空" }
        ],
      }
    };
  },
  created() {
    const nowDate=new Date();
    this.daterangeFlowDate[0]=this.parseTime(new Date(),"{y}-{m}-{d}");
    this.daterangeFlowDate[1]=this.parseTime(new Date(nowDate.setDate(nowDate.getDate()+1)),"{y}-{m}-{d}");
    this.getList();
    this.getListStore();
    this.getListBank();
    this.getListBankOutcome();
    this.getListBankOutcomeName();
    this.getListBankProject();
  },
  methods: {
    /** 查询每日营业出账流水列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeFlowDate && '' != this.daterangeFlowDate) {
        this.queryParams.params["beginFlowDate"] = this.daterangeFlowDate[0];
        this.queryParams.params["endFlowDate"] = this.daterangeFlowDate[1];
      }
      listReportOutFlow(this.queryParams).then(response => {
        this.reportOutFlowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListStore(){
      listStore(null).then((response) =>{
        this.storeListOptions = response.rows;
      });
    },
    getListBank(){
      listBank(null).then((response) =>{
        this.bankListOptions = response.rows;
      });
    },
    getListBankOutcome(){
      listBankOutcome(null).then((response) =>{
        this.bankOutcomeListOptions = response.rows;
      });
    },
    getListBankOutcomeName(){
      listBankOutcomeName(null).then((response) =>{
        this.bankOutcomeNameListOptions = response.rows;
        
      });
    },
    getListBankProject(){
      listBankProject(null).then((response) =>{
        this.bankProjectListOptions = response.rows;
        console.log(this.bankProjectListOptions)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flowDate: null,
        storeId: null,
        projectId: null,
        bankId: null,
        outcomeId: null,
        outcomeNameId: null,
        price: null,
        userName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeFlowDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.flowDate=new Date();
      this.open = true;
      this.title = "添加每日营业出账流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReportOutFlow(id).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.open = true;
        this.title = "修改每日营业进账流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReportOutFlow(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReportOutFlow(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除每日营业进账流水编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReportOutFlow(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有每日营业进账流水数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportReportOutFlow(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
